#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int DataType;
typedef struct Node
{
	DataType data;
	struct Node *next;
} SLNode;
int ListInitiate(SLNode **head)
{
	*head = (SLNode *)malloc(sizeof(SLNode));
	(*head)->next=NULL;
	if(*head == NULL)
		return 1;
	else
		return 0;
}
int ListLength(SLNode *head)
{
	int len=0;
	for (len = 0 ; head ->next != NULL; len++)
		head = head->next;
	return len;
}
int ListInsert(SLNode *head, int i, DataType x)
{
	SLNode *p,*q;
	int j;
	p = head;
	for(j = -1;p ->next != NULL && j < (i-1);j++)
		p = p ->next;
	if(j != i-1){
		printf("插入元素位置参数错！");
		return 1;
	}
	q = (SLNode *) malloc(sizeof(SLNode));
	q ->data = x;
	q ->next = p ->next;
	p ->next = q;
	return 0;
}
int ListDelete(SLNode *head, int i, DataType *x)
{
	SLNode *p,*s;
	int j;
	p = head;
	for(j = -1; p ->next != NULL && p ->next->next != NULL && j < i-1 ; j++)
		
		p = p ->next;
	if(j != i-1){
		printf("删除数据元素位置参数错误！");
		return 0;
	}
	s = p ->next;
	*x = s ->data;
	p ->next = p ->next->next;
	free(s);
	return 1;
}
int ListGet(SLNode *head, int i, DataType *x)
{
	int j = -1;
	SLNode *p;
	p = head;
	while(p ->next != NULL && j < i){
		p = p -> next;
		j++;
	}
	while(j != i){
		printf("i的值不合法！");
		return 1;
	}
	*x = p ->data;
	return 0;
}
int Destroy(SLNode **head)
{
	SLNode *p, *q;
	p = *head;
	while(p != NULL)
	{
		q = p;
		p = p->next;
		free(q);
	}
	*head = NULL;
	return 0;
}
